﻿<%@ Page Language="C#" MasterPageFile="~/default2.master" Inherits="System.Web.UI.Page" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Net.Mail" %>
<%@ Import Namespace="System.Web.UI" %>

<script type="text/C#" language="CS" runat="server">
    const string ConfirmMessage = "Thank you for supporting CTEF!";
    const string ErrorMessage = "There has been an error with your submission. Please use 'Back' button to make sure all fields have been filled out. If the error persists, please contact webmaster directly.";
    
    bool IsValidInput
    {
        get 
        {
            decimal result;
            return
                !string.IsNullOrEmpty(txtName.Text.Trim()) &&
                !string.IsNullOrEmpty(txtEmail.Text.Trim()) &&
                decimal.TryParse(txtAmount.Text, out result);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            lblMessage.Text = string.Empty;
            lblMessage.Visible = false;
            pnlPledge.Visible = true;
            btnSubmit.Click += new EventHandler(btnSubmit_Click);
        }
        else
        {
            lblMessage.Visible = true;
            pnlPledge.Visible = false;
        }
    }
    
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        lblMessage.Text = ErrorMessage;
        lblMessage.ForeColor = Color.Red;
        
        if (IsValidInput)
        {
            var subject = ComposeEmailSubject();
            var body = ComposeEmailBody();

            if (SendEmail(subject, body))
            {
                lblMessage.Text = ConfirmMessage;
                lblMessage.ForeColor = Color.Green;
            }
        }
    }

    string ComposeEmailSubject()
    {
        return "[CTEF donation] new pledge from " + txtName.Text.Trim();
    }

    string ComposeEmailBody()
    {
        var buffer = new StringBuilder();
        buffer.Append("Name: "); buffer.AppendLine(txtName.Text.Trim());
        buffer.Append("Email: "); buffer.AppendLine(txtEmail.Text.Trim());
        buffer.Append("Phone: "); buffer.AppendLine(txtPhone.Text.Trim());
        buffer.Append("Company: "); buffer.AppendLine(txtCompany.Text.Trim());
        buffer.Append("Amount: "); buffer.AppendLine(txtAmount.Text.Trim());
        buffer.Append("Program(s): "); buffer.AppendLine(txtPrograms.Text.Trim());        
        
        return buffer.ToString();
    }   

    bool SendEmail(string mailSubject, string mailBody)
    {
        bool emailSent = false;

        try
        {
            var message = new MailMessage();
            var smtp = new SmtpClient();

            message.From = new MailAddress("china.tomorrow@ctef.org", "China Tomorrow Education Foundation");

            // TODO: update to correct email accounts before going live
            message.To.Add(new MailAddress("haonantan@gmail.com"));
            message.To.Add(new MailAddress("yileiw@efura.com"));            

            message.IsBodyHtml = false;
            message.Subject = mailSubject;
            message.Body = mailBody;

            smtp.Host = "relay-hosting.secureserver.net";
            smtp.Port = 25;
            smtp.UseDefaultCredentials = false;

            // TODO: is there a way to hide plain text credentials?
            smtp.Credentials = new NetworkCredential("china.tomorrow@ctef.org", "ctef2007");
            smtp.Send(message);
            emailSent = true;
        }
        catch (Exception ex) { lblMessage.Text = ex.Message; }

        return emailSent;
    }
</script>

<asp:Content ContentPlaceHolderID="LeftContent" runat="Server">
    <div id="breadcrumbdiv">
        Get Involved &gt; Donate</div>
    <p>
      <asp:Label ID="lblMessage" runat="server"></asp:Label>
    </p>
    
    <h2>
        YOUR DONATION MAKES A DIFFERENCE</h2>
    <hr />
    <p>
        Did you know? It only costs:</p>
    <ul>
    <strong>
        <li>$1 to supply a book for the school library</li>
        <li>$15 to $20 to supply a set of desk and chair</li>
        <li>$150 to support a high school student for a half year tuition and fees</li>
        <li>$400 to support a college student for a half year tuition and fees</li>
        <li>$2,500 to $3,000 to build a standard classroom</li>
        <li>$6,000 - $10,000 to build a single-room rural school </li>
        <li>$30,000 - $60,000 to build a village school that hosts 100-300 students </li>
    </strong>
    </ul>
    <p>
        With $6,000 or more in donation, we will name a school after a group or individual
        donor(s). Give the gift of education and change someone's life!</p>
    <h2>
        A FEWS WAYS TO MAKE A DONATION</h2>
    <hr />
    <h3>
        Donate Now</h3>
    <p>
        Click the DONATE NOW button, and choose a program to support via our donor tool.
        Does your employer match your charitable contributions? If so, please remember to register your
        contribution with them to double your impact! 
    </p>
    <a href="https://ctef.donortools.com/"<img src="http://www.ctef.org/test/images/donatenow.jpg" alt="Donate Now" /></a>	
    
    <h3>
        Pledge Now</h3>
    
    <asp:Panel ID="pnlPledge" runat="server">
	  <form name="form_pledge" runat="server">
            	<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" >
                  <tr>
                    <td width="20%" height="30" >*Name:</td>
                    <td width="50%" >
                      <asp:TextBox ID="txtName" runat="server" TextMode="SingleLine" Size="30"></asp:TextBox>                    
                    </td>
                  </tr>
                  <tr>
                    <td height="30">*Email:</td>
                    <td>
                      <asp:TextBox ID="txtEmail" runat="server" TextMode="SingleLine" Size="30"></asp:TextBox>
                    </td>
                  </tr>
                  <tr>
                    <td height="30" >Telephone: </td>
                    <td ><asp:TextBox ID="txtPhone" runat="server" TextMode="SingleLine" Size="30"></asp:TextBox></td>
                  </tr>
                   <tr>
                    <td height="30" >Company: </td>
                    <td ><asp:TextBox ID="txtCompany" runat="server" TextMode="SingleLine" Size="30"></asp:TextBox></td>
                  </tr>
                    <tr>
                    <td height="30" >*Pledge Amount: </td>
                    <td ><asp:TextBox ID="txtAmount" runat="server" TextMode="SingleLine" Size="30"></asp:TextBox></td>
                  </tr> 
                  <tr>
                    <td colspan="2" ><p>Please briefly state which program(s) you'd like to support:</p></td>
                    </tr>
                  <tr>
                    <td colspan="2">
                      <asp:TextBox ID="txtPrograms" runat="server" TextMode="MultiLine" 
                                   Columns="65" Rows="6" Size="1000"></asp:TextBox>
                    </td>
                  </tr>                 
                  <tr>
                    <td height="45" colspan="2"  align="center">
                        <asp:Button ID="btnSubmit" runat="server" Text="Submit" 
                            onclick="btnSubmit_Click" />
                   </td>
                  </tr>
            </table>
        </form> 
    </asp:Panel>
    <h3>
        Mail your check to CTEF</h3>
    <p>
        <strong>China Tomorrow Education Foundation</strong><br />
        P.O. Box 1103<br />
        Mercer Island, WA 98040
    </p>
</asp:Content>
<asp:Content ContentPlaceHolderID="RightContent" runat="Server">
</asp:Content>
